package com.wjgl.action;

import java.util.HashSet;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;
import com.wjgl.dao.RoleHasPermissionDAO;
import com.wjgl.domain.Permission;
import com.wjgl.domain.Role;
import com.wjgl.domain.RoleHasPermission;
import com.wjgl.service.PermissionService;
import com.wjgl.service.RoleHasPermissionService;
import com.wjgl.service.RoleService;
import com.wjgl.service.SqlHelper;

public class AuthorizeAction extends ActionSupport {
	private RoleService roleService;
	private PermissionService permissionService;
	private RoleHasPermissionService roleHasPermissionService;
	private String ids;
	private Integer roleId;

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
	

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public RoleHasPermissionService getRoleHasPermissionService() {
		return roleHasPermissionService;
	}

	public void setRoleHasPermissionService(
			RoleHasPermissionService roleHasPermissionService) {
		this.roleHasPermissionService = roleHasPermissionService;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
//		Role role =  roleService.findRoleById(roleId);
//		Set<RoleHasPermission> rhps = role.getRoleHasPermissions();
//		System.out.println("size="+ rhps.size());
//		rhps.clear();
//		System.out.println("size="+ rhps.size());
		
		String sql = "delete from role_has_permission where role_id="+roleId;
		SqlHelper sh = new SqlHelper();
		sh.excuteSql(sql);
		String[] idstmp = ids.split(",");
		for(int i=0; i<idstmp.length-1; i++){
			sql = "insert into role_has_permission values("+roleId+", "+idstmp[i]+")";
			sh.excuteSql(sql);
		}
		
//		String[] idstmp = ids.split(",");
//		Permission p=null;
//		RoleHasPermission rhp = null;
//		for(int i=0; i<idstmp.length-1; i++){
//			System.out.println("id= "+idstmp[i]);
//			p = permissionService.findPermissionById(Integer.parseInt(idstmp[i]));
//			rhp = new RoleHasPermission();
//			rhp.setRole(role);
//			rhp.setPermission(p);
//			rhps.add(rhp);
//		}
		//role.setRoleHasPermissions(rhps);
//		roleService.updateRole(role);
		//System.out.println(ids);
		return null;
	}
	
}
